package Test;

import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        Solution  solution =new Solution();
        solution.calculate("3+2*2");
    }

}
class Solution {
    public String removeDuplicates(String s) {
        char [] c =s.toCharArray();
        Stack<Character> stack=new Stack();
        for(int i=0;i<c.length;i++) {
            if(stack.empty()) {
                stack.push(c[i]);
            } else {
                while(stack.peek().equals(c[i])) {
                    stack.pop();
                    i++;
                    if(i>=c.length) {
                        break;
                    }
                }
                if(i<c.length) {
                    stack.push(c[i]);
                }

            }
        }
        StringBuilder rs =new  StringBuilder();
        while(!stack.empty()) {
            rs.append(stack.pop());
        }
        return rs.toString();
    }
    public boolean backspaceCompare(String s, String t) {
        return changeStr(s).equals(changeStr(t));
    }

    public String changeStr(String str) {
        char [] c =str.toCharArray();
        StringBuffer ret =new  StringBuffer();
        for(char cc : c) {
            if(cc=='#') {
                if(ret.length()>0){
                    ret.deleteCharAt(ret.length()-1);
                }

            } else {
                ret.append(cc);
            }
        }
        return ret.toString();
    }

    public int calculate(String s) {

        char [] c=s.toCharArray();
        StringBuffer sb =new StringBuffer();
        int sum=0;
        for(char cc : c) {
            if('0'<=cc&&cc<='9') {
                sb.append(cc);
            } else {
                sum=Op(cc,sb,sum);
                sb.delete(0,sb.length());
            }
        }
        return sum;
    }
    public int Op(char cc,StringBuffer sb,int sum) {
        int temp=Integer.parseInt(sb.toString());
        switch(cc) {
            case '+' : return temp+sum;
            case '-' : return temp-sum;
            case '*' : return temp*sum;
            case '/' : return temp/sum;
        }
        return 0;
    }


}